package blog.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import blog.entity.Article;
import blog.entity.Category;
import blog.entity.Tag;
import blog.mapper.ArticleCateMapper;
import blog.mapper.ArticleMapper;
import blog.mapper.ArticleTagMapper;

/**
 * 文章Servie实现
 */
@Service
public class ArticleServiceImpl implements ArticleService {
	@Resource
	private ArticleMapper articleMapper;

	@Resource
	private ArticleCateMapper articleCateMapper;
	
	@Resource
	private ArticleTagMapper articleTagMapper;

	/**
	 * 分页查询文章列表(同时把某个文章所属哪些分类也查出来了) 要注意,实现分页查询,需要在mybatis的主配置文件中配置分页插件
	 */
	public PageInfo<Article> getPageArticleList(Integer pageIndex, Integer pageSize, HashMap<String, Object> paramMap) {
		PageHelper.startPage(pageIndex, pageSize);
		List<Article> articleList = articleMapper.findAll(paramMap);

		for (int i = 0; i < articleList.size(); i++) {
			// 封装CategoryList
			List<Category> categoryList = articleCateMapper.listCategoryByArticleId(articleList.get(i).getArticleId());
			if (categoryList == null || categoryList.size() == 0) {
				categoryList = new ArrayList<>();
				categoryList.add(Category.getDefault());
			}
			articleList.get(i).setCategoryList(categoryList);
		}

		return new PageInfo<Article>(articleList);
	}

	/**
	 * 统计文章数量,对前台来说,只知道查询已发布的文章
	 */
	public Integer countArticle() {
		return articleMapper.countArticle();
	}

	/**
	 * 统计总的浏览次数
	 */
	public Integer countArticleView() {
		return articleMapper.countArticleView();
	}

	/**
	 * 得到最后更新的文章(目前只用到了它的更新日期)
	 */
	public Article getLastUpdateArticle() {
		return articleMapper.getLastUpdateArticle();
	}

	/**
	 * 根据id查询文章
	 */
	public Article getArticleById(Integer id) {
		Article article = articleMapper.getArticleById(id);
		if (article != null) {
			List<Category> categoryList = articleCateMapper.listCategoryByArticleId(article.getArticleId());
			article.setCategoryList(categoryList);

			List<Tag> tagList = articleTagMapper.listTagByArticleId(article.getArticleId());
			article.setTagList(tagList);
		}
		return article;
	}

	/**
	 * 查询属于某个分类的前n条文章
	 */
	public List<Article> listArticleByCategoryId(Integer categoryId, int n) {
		return articleMapper.listArticleByCategoryId(categoryId, n);
	}

	/**
	 * 查出前n条访问量最高的文章
	 * 
	 * @param n
	 * @return 文章列表
	 */
	public List<Article> listArticleByViewCount(Integer n) {
		return articleMapper.listArticleByViewCount(n);
	}

	/**
	 * 下一个文章
	 */
	public Article getAfterArticle(Integer articleId) {
		return articleMapper.getAfterArticle(articleId);
	}

	/**
	 * 上一个文章
	 */
	public Article getPreArticle(Integer articleId) {
		return articleMapper.getPreArticle(articleId);
	}

	/**
	 * 随机取出前n条文章
	 */
	public List<Article> listRandomArticle(int n) {
		return articleMapper.listRandomArticle(n); 
	}

	/**
	 * 获取前n条热评文章
	 */
	public List<Article> listArticleByCommentCount(int n) {
		return articleMapper.listArticleByCommentCount(n); 
	}

	/**
	 * 更新文章
	 */
	public void updateArticle(Article article) {
		articleMapper.updateArticle(article);
	}

	/**
	 * 更新文章的评论数
	 */
	public void updateCommentCount(Integer articleId) {
		articleMapper.updateCommentCount(articleId);
		
	}
	

}
